package io.adrainty.bolee.trade.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import io.adrainty.bolee.framework.commons.utils.JsonUtil;
import io.adrainty.bolee.trade.mapper.IPayChannelMapper;
import io.adrainty.bolee.trade.models.bo.PayChannelBO;
import io.adrainty.bolee.trade.models.entity.OtherConfigEntity;
import io.adrainty.bolee.trade.models.entity.PayChannelEntity;
import io.adrainty.bolee.trade.repository.IPayChannelRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/31 下午10:56
 */

@Repository
public class PayChannelRepositoryImpl implements IPayChannelRepository {

    @Resource
    private IPayChannelMapper payChannelMapper;

    @Override
    public IPage<PayChannelEntity> pageData(Integer pageNum, Integer pageSize, PayChannelEntity param) {
        Wrapper<PayChannelBO> wrapper = getWrapper(param);
        Page<PayChannelBO> page = new Page<>(pageNum, pageSize);
        return payChannelMapper.selectPage(page, wrapper).convert(PayChannelRepositoryImpl::convertPayChannel);
    }

    @Override
    public PayChannelEntity findOne(PayChannelEntity param) {
        Wrapper<PayChannelBO> wrapper = getWrapper(param);
        PayChannelBO channelBO = payChannelMapper.selectOne(wrapper);
        return convertPayChannel(channelBO);
    }

    private Wrapper<PayChannelBO> getWrapper(PayChannelEntity param) {
        return Wrappers.lambdaQuery(PayChannelBO.class)
                .eq(param.getId() != null, PayChannelBO::getId, param.getId())
                .likeRight(StringUtils.isNotBlank(param.getChannelName()), PayChannelBO::getChannelName, param.getChannelName())
                .eq(StringUtils.isNotBlank(param.getChannelLabel()), PayChannelBO::getChannelLabel, param.getChannelLabel())
                .eq(param.getDataState() != null, PayChannelBO::getDataState, param.getDataState())
                .orderByDesc(PayChannelBO::getCreatedTime);
    }

    private static PayChannelEntity convertPayChannel(PayChannelBO item) {
        if (item == null) return null;
        PayChannelEntity bean = BeanUtil.toBean(item, PayChannelEntity.class, CopyOptions.create().setIgnoreProperties("otherConfigs"));
        if (StringUtils.isNotBlank(bean.getOtherConfig())) {
            String otherConfig = bean.getOtherConfig();
            List<OtherConfigEntity> otherConfigs = JsonUtil.fromJson(otherConfig, new TypeReference<>() {
            });
            bean.setOtherConfigs(otherConfigs);
        } else {
            bean.setOtherConfigs(List.of());
        }
        return bean;
    }

}
